/*
 * Copyright 2008 Max Kugland
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.splink.library.tween.sprop {
	import flash.display.DisplayObject;
	import flash.geom.ColorTransform;

	/**
	 * Enables the tweening of a <code>DisplayObject</code>'s <code>ColorTransform</code> properties.
	 * 
	 * @author Max Kugland
	 */
	public class ColorProp implements ISpecialProp {
		public static const COLOR : String = "color";

		private var _colorTransform : ColorTransform;

		/**
		 * @param display the <code>DisplayObject</code> whose <code>colorTransform</code> properties should change
		 */
		public function ColorProp(display : DisplayObject) {
			_colorTransform = display.transform.colorTransform;
		}

		/**
		 * @inheritDoc
		 */
		public function apply(target : Object, prop : String, start : Number, end : Number, value : Number, percent : Number) : void {
			if(target is DisplayObject) {
				_colorTransform[prop] = mixHex(start, end, percent);
				DisplayObject(target).transform.colorTransform = _colorTransform;
			}
		}

		private static function mixHex(hex : int, hex2 : int, ratio : Number) : int {
			var r : int = hex >> 16;
			var g : int = hex >> 8 & 0xFF;
			var b : int = hex & 0xFF;
			r += ((hex2 >> 16) - r) * ratio;
			g += ((hex2 >> 8 & 0xFF) - g) * ratio;
			b += ((hex2 & 0xFF) - b) * ratio;
			
			return(r << 16 | g << 8 | b);
		}

		/**
		 * @inheritDoc
		 */
		public function getValue(prop : String) : Number {
			return _colorTransform[prop];
		}
	}
}
